<?php

    //$wechatObj = new ZipayAction();




    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    if (!empty($postStr)){

                    $time=time();
                    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

                    $fromUsername = $postObj->FromUserName;

                    $toUsername = $postObj->ToUserName;

                    $keyword = trim($postObj->Content);
                $Event = trim($postObj->Event);

                $EventKey = trim((string)$postObj->EventKey);

                    $keyArray = explode("_", $EventKey);



                    $textTpl = "<xml>  
                  
                            <ToUserName><![CDATA[%s]]></ToUserName>  
                  
                            <FromUserName><![CDATA[%s]]></FromUserName>  
                  
                            <CreateTime>%s</CreateTime>  
                  
                            <MsgType><![CDATA[%s]]></MsgType>  
                  
                            <Content><![CDATA[%s]]></Content>  
                  
                            <FuncFlag>0<FuncFlag>  
                  
                            </xml>";

                if(!empty($EventKey)){


                                    /**
                                    ToUserName	开发者微信号
                                    FromUserName	发送方帐号（一个OpenID）
                                    CreateTime	消息创建时间 （整型）
                                    MsgType	消息类型，event
                                    Event	事件类型，subscribe
                                    EventKey	事件KEY值，qrscene_为前缀，后面为二维码的参数值
                                    Ticket	二维码的ticket，可用来换取二维码图片
                                     */




                                    //扫码关注

                                    if(count($keyArray) <> 1){

                                        $fck=M('fck');

                                        $rid=$keyArray[1];
                                        $mapp  = array();
                                        $mapp['id'] = $rid;


                                        $authInfoo = $fck->where($mapp)->find();
                                        $name= $authInfoo['user_id'];

                                        $find_appid=$fck->where("appid='".$fromUsername."'")->find();

                                        if(!$find_appid){

                                            create_user: $mynn = ''.rand(1000000,9999999);

                                            $fwhere['user_id'] = $mynn;
                                            $frss = $fck->where($fwhere)->field('id')->find();
                                            if ($frss){
                                                goto create_user;
                                            }else{
                                               $user_id = $mynn;
                                            }

                                            $app_id = $fromUsername;
                                            $data=array();
                                            $data['user_id'] = $mynn;
                                            $data['appid']   = $app_id;
                                            $data['re_path'] = $authInfoo['re_path'].$authInfoo['id'].',';  //推荐路径
                                            $data['re_id'] = $authInfoo['id'];                              //推荐人ID
                                            $data['re_name'] = $authInfoo['user_id'];
                                            $data['u_level'] = 0;
                                            $data['pdt']     = $time;
                                            $data['is_pay']  = 1;
                                            $data['re_level'] = $authInfoo['re_level'] + 1;                 //代数(绝对层数)

                                            $password = 1;
                                            $password2 = 2;
                                            $data['password']            = md5($password);  //一级密码加密
                                            $data['passopen']            = md5($password2);  //二级密码加密
                                            $data['pwd1']                = $password;       //一级密码不加密
                                            $data['pwd2']                = $password2;       //二级密码不加密
                            //                $ans = $fck->add($data);
                                           $ans =  $fck->execute("insert into __TABLE__ (re_name,re_path,re_id,pdt,u_level,user_id,is_pay,re_level,appid,password,passopen,pwd1,pwd2) values ('".$data['re_name']."','".$data['re_path']."',".$data['re_id'].",".$time.",1,'".$mynn."',1,".$data['re_level'].",'".$app_id."','".$data['password'] ."','".$data['passopen'] ."',1,2)");

                            //                $fck->execute('update __TABLE__ set re_nums=re_nums+1 where id='.$rid);

                            //                if ($ans){
                                                $msgType = "text";

                                                $contentStr = '欢迎您关注微利商行，您的推荐人是 '.$name.'! 您可以使用账户：'.$user_id.' 密码：'.$password.' 二级密码：'.$password2.' 来登陆微利商行系统！';

                                                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                                                echo $resultStr;
                            //                }

                                        }else{
                                            $msgType = "text";

                                            $contentStr = '欢迎您回到微利商行! 您可以使用账户：'.$find_appid['user_id'].' 密码：'.$find_appid['pwd1'].' 二级密码：'.$find_appid['pwd2'].' 来登陆微利商行系统！';

                                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                                            echo $resultStr;
                                        }




                                    }


                }else{

                            //直接公众号搜索关注

                            if($Event=='subscribe'){

                                $fck=M('fck');

                                $rid=$keyArray[1];
                                $mapp  = array();
                                $mapp['id'] = 1;


                                $authInfoo = $fck->where($mapp)->find();
                                $name= $authInfoo['user_id'];

                                $find_appid=$fck->where("appid='".$fromUsername."'")->find();

                                if(!$find_appid){

                                    create_user1: $mynn = ''.rand(1000000,9999999);

                                    $fwhere['user_id'] = $mynn;
                                    $frss = $fck->where($fwhere)->field('id')->find();
                                    if ($frss){
                                        goto create_user1;
                                    }else{
                                        $user_id = $mynn;
                                    }

                                    $app_id = $fromUsername;
                                    $data=array();
                                    $data['user_id'] = $mynn;
                                    $data['appid']   = $app_id;
                                    $data['re_path'] = $authInfoo['re_path'].$authInfoo['id'].',';  //推荐路径
                                    $data['re_id'] = $authInfoo['id'];                              //推荐人ID
                                    $data['re_name'] = $authInfoo['user_id'];
                                    $data['u_level'] = 0;
                                    $data['pdt']     = $time;
                                    $data['is_pay']  = 1;
                                    $data['re_level'] = $authInfoo['re_level'] + 1;                 //代数(绝对层数)

                                    $password = 1;
                                    $password2 = 2;
                                    $data['password']            = md5($password);  //一级密码加密
                                    $data['passopen']            = md5($password2);  //二级密码加密
                                    $data['pwd1']                = $password;       //一级密码不加密
                                    $data['pwd2']                = $password2;       //二级密码不加密
                                    //                $ans = $fck->add($data);
                                    $ans =  $fck->execute("insert into __TABLE__ (re_name,re_path,re_id,pdt,u_level,user_id,is_pay,re_level,appid,password,passopen,pwd1,pwd2) values ('".$data['re_name']."','".$data['re_path']."',".$data['re_id'].",".$time.",1,'".$mynn."',1,".$data['re_level'].",'".$app_id."','".$data['password'] ."','".$data['passopen'] ."',1,2)");

                                    //                $fck->execute('update __TABLE__ set re_nums=re_nums+1 where id='.$rid);

                                    //                if ($ans){
                                    $msgType = "text";

                                    $contentStr = '欢迎您关注微利商行，您的推荐人是 '.$name.'! 您可以使用账户：'.$user_id.' 密码：'.$password.' 二级密码：'.$password2.' 来登陆微利商行系统！';

                                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                                    echo $resultStr;
                                    //                }

                                }else{
                                    $msgType = "text";

                                    $contentStr = '欢迎您回到微利商行! 您可以使用账户：'.$find_appid['user_id'].' 密码：'.$find_appid['pwd1'].' 二级密码：'.$find_appid['pwd2'].' 来登陆微利商行系统！';

                                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                                    echo $resultStr;
                                }


                            }
                }

    }else {

        echo '咋不说哈呢';

        exit;

    }





?>